public class 验证IP地址 {
    // 屎山代码, 面对对象编程
    public String validIPAddress(String queryIP) {
        // 先分割
        String[] queryIPs = queryIP.split("\\.",-1);
        int n = queryIPs.length;
        // 初步筛选 ip
        if (n == 4) {
            for (int i = 0; i < n; i++) {
                // 判断是否有非法字符
                String str = queryIPs[i];
                if(str.equals("") || checkIPv4(str)) return "Neither";
                // 拿到当前值
                int cur = Integer.parseInt(str);
                if (cur > 255 || (queryIPs[i].charAt(0) == '0' && cur != 0) || (cur == 0 && str.length() >=2 && queryIPs[i].charAt(1) == '0')) {
                    return "Neither";
                }
            }
            return "IPv4";
        }
        queryIPs = queryIP.split("\\:",-1);
        n  = queryIPs.length;
        if (n == 8) {
            for (int i = 0; i < n; i++) {
                // 拿到当前字符串
                String  cur = queryIPs[i];
                if (cur.length() > 4 || cur.length() < 1 || find(cur)) {
                    return "Neither";
                }
            }
            return "IPv6";
        } else {
            return "Neither";
        }
    }

    private boolean checkIPv4(String cur) {
        // 判断是否有非法字符
        if (cur.length() > 4) {
            return true;
        }
        for (int i = 0; i < cur.length(); i++) {
            char ch = cur.charAt(i);
            if (!(ch >= '0' && ch <= '9')) {
                return true;
            }
        }
        return false;
    }

    private boolean find(String cur) {
        // 判断是否有非法字符
        for (int i = 0; i < cur.length(); i++) {
            char ch = cur.charAt(i);
            if (!((ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F') || (ch >= '0' && ch <= '9'))) {
                return true;
            }
        }
        return false;
    }
}
